import { GObject } from "fairygui-cc";

export class UIItem {
    private _view!: GObject | null;
    protected data!: any;
    protected args!: any;

    public get view(): GObject | null {
        return this._view;
    }

    /**
     * 绑定对象
     * @param view 
     * @param data 
     * @param args 
     */
    public attach(view: GObject, data: any, args: any) {
        this._view = view;
        this.data = data;
        this.args = args;
        this.onStart();
    }

    /**
     * 刷新数据
     * @param data 
     * @param args 
     */
    public refresh(data: any, args: any) {
        this.data = data;
        this.args = args;
        this.onStart();
    }

    /**
     * 销毁
     */
    public destroy() {
        this.onDestroy();
        if (this._view) {
            let view: any = this._view;
            delete view['_itemHandler'];
            this._view = null;
        }
    }

    /**
     * 第一次启动的时候回调用
     */
    protected onStart() {

    }

    /**
     * 窗口销毁的时候调用
     */
    protected onDestroy() {

    }
}